package cn.jiyun.controller;

import cn.jiyun.entity.Goods;
import cn.jiyun.service.GoodsService;
import cn.jiyun.vo.GoodsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
//分页查询
@RequestMapping("/findAll")
public String findAll(@RequestParam(defaultValue = "1") Integer pageNum, HttpServletRequest request){
    PageHelper.startPage(pageNum,2);
    List<GoodsVo> list = goodsService.findAll();
    PageInfo page = new PageInfo(list);
    request.setAttribute("page",page);
    return "show";
}
    @RequestMapping("toAdd")
    public String  toAdd(HttpServletRequest request){
        return "add";

    }
    @RequestMapping("addgoods")
    public  String addgoods(Goods goods){
        goodsService.addgoods(goods);
        return "redirect:findAll";
    }
    @RequestMapping("toEdit")
    public String toedid(Integer id,HttpServletRequest request){
        Goods good=goodsService.toedit(id);
        System.out.println(good.toString());
       request.setAttribute("good",good);
       return "edit";
    }
    @RequestMapping("editgoods")
    public String editgoods(Goods goods){
        goodsService.editgoods(goods);
        return "redirect:findAll";
    }
    @RequestMapping("tos")
    public String tos(Integer id){
        goodsService.tos(id);
        return "redirect:findAll";
    }
    @RequestMapping("tox")
    public String tox(Integer id){
        goodsService.tox(id);
        return "redirect:findAll";
    }
    @RequestMapping("del")
    public  String del(Integer id){
        goodsService.del(id);
        return "redirect:findAll";
    }
}
